import { CanActivate, ExecutionContext, HttpException, Injectable, Inject } from '@nestjs/common';
import { Observable } from 'rxjs';
import { AuthWhiteList } from 'src/utils/config';
import { TakeToken } from 'src/utils/token';
@Injectable()
export class CommonGuard implements CanActivate {
  async canActivate(
    context: ExecutionContext,
  ): Promise<any>{
    const request = context.switchToHttp().getRequest();
    const { token } = request.headers
    const index = AuthWhiteList.indexOf(request.url)
    if(index >= 0){
      return true
    }
    if(!token){
      throw new HttpException("没有令牌",501)
    }
    const takeToken = await TakeToken(token, 'token')
    if(!takeToken){
      throw new HttpException("令牌已失效",501)
    }
    return true
    
  }
}
